import serve from './request.js';

/**
 * 支付方法
 * @param {Object} payData 提交参数
 *  {
 *      appkey: uni.getStorageSync('appkey'),
 *      'access-token': uni.getStorageSync('access-token'),
 *      pay_type: '', 付款类型
 *      pay_id: 0,   付款ID
 *      is_use_hdq: 0, 是否使用互动券
 *      is_use_judge: 0, 是否使用品鉴金
 *      payment_type: 1,  支付方式 1：微信 5：余额
}
 * @param {String} jumpFile
 * @param {String} jumpType 调整类型 1：文件 2：方法
 */
var payFun = function(payData, jumpFile, jumpType = 1) {
    if(payData.payment_type == 5) {
        moneyPayFun(payData, jumpType, jumpFile);
        return ;
    }
    if(payData.payment_type == 1) {
        wechatPayFun(payData, jumpType, jumpFile);
        return ;
    }
    uni.showToast({
    	title: '错误的支付方法',
    	icon: 'none',
    	success() {
			if(jumpType == 1) {
				setTimeout(function() {
				    uni.redirectTo({
				        url: jumpFile
				    });
				}, 2000);
			} else {
				jumpFile.callbackFun();
			}
    	}
    });
    return ;
}

var moneyPayFun = function(payData, jumpType, jumpFile) {
    uni.showLoading({
    	title: '支付中···',
    	mask: true
    });
    serve.request({
    	url: '/pay/money-pay',
    	data: payData,
        v: '/v3'
    }, 'POST').then(res => {
        uni.hideLoading();
        uni.showToast({
        	title: res.message,
        	icon: 'none',
        	success() {
        		if(res.code == 200){
                    if(jumpType == 1) {
                    	setTimeout(function() {
                    	    uni.redirectTo({
                    	        url: jumpFile
                    	    });
                    	}, 2000);
                    } else {
                    	jumpFile.callbackFun();
                    }
        		}
				return ;
        	}
        });
    });
}
var wechatPayFun = function(payData, jumpType, jumpFile) {
    console.info(payData);
    uni.showLoading({
    	title: '唤起支付中···',
    	mask: true
    });
    serve.request({
    	url: '/pay/wechat-pay',
    	data: payData,
        v: '/v3'
    }, 'POST').then(res => {
        uni.hideLoading();
        if(res.code != 200){
            uni.showToast({
                title: res.message,
                icon: 'none',
            });
            return ;
        }
        let payId = res.data.payId;
        let payConfig = res.data.payConfig;
        uni.requestPayment({
        	provider: 'wxpay',
        	timeStamp: payConfig.timeStamp,
        	nonceStr: payConfig.nonceStr,
        	package: payConfig.package,
        	signType: payConfig.signType,
        	paySign: payConfig.paySign,
        	success: function(res) {
        		queryPayFun(payId, jumpType, jumpFile);
        		return;
        	},
        	fail: function(err) {
        		uni.showToast({
        			title: '放弃支付',
        			icon: 'none',
        			success() {
                        // queryPayFun(payId, jumpType, jumpFile);
                        if(jumpType == 1) {
                        	setTimeout(function() {
                        	    uni.redirectTo({
                        	        url: jumpFile
                        	    });
                        	}, 2000);
                        } else {
                        	jumpFile.callbackFun();
                        }
        			}
        		});
        		return;
        	}
        })
    });
}
var queryPayFun = function(payId, jumpType, jumpFile = '', num = 20) {
	var _self = this;
	uni.showLoading({
		title: '支付结果查询中···',
		mask: true
	});
	if(num <= 0) {
		uni.showToast({
			title: '未接受到支付成功请求，请联系店铺工作人员！',
			icon: 'none',
			success() {
				if(jumpType == 1) {
					setTimeout(function() {
					    uni.redirectTo({
					        url: jumpFile
					    });
					}, 2000);
				} else {
					jumpFile.callbackFun();
				}
			}
		});
		return ;
	}
	setTimeout(function() {
		serve.request({
			url: '/pay/query-pay',
			data:{
				appkey: uni.getStorageSync('appkey'),
				payId: payId,
			},
            v: '/v2'
		}).then(res => {
			if(res.data.payStatus == 1) {
				uni.showToast({
					title: '支付成功',
					icon: 'none',
					success() {
                        if(jumpType == 1) {
                        	setTimeout(function() {
                        	    uni.redirectTo({
                        	        url: jumpFile
                        	    });
                        	}, 2000);
                        } else {
                        	jumpFile.callbackFun();
                        }
						return ;
					}
				});
			} else {
				num --;
				queryPayFun(payId, jumpType, jumpFile, num);
			}
		});
	}, 1500);
};

module.exports = {
    payFun: payFun,
    moneyPayFun: moneyPayFun,
    wechatPayFun: wechatPayFun,
    queryPayFun : queryPayFun,
};